﻿namespace Smart.Utils.System2
{
    using System;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;

    public class CSysMsgBoxHook
    {
        private IntPtr _hook;
        private const string BUT_ABORT = "Huỷ";
        private const string BUT_CANCLE = "Th\x00f4i";
        private const string BUT_IGNORE = "Bỏ qua";
        private const string BUT_NO = "Kh\x00f4ng";
        private const string BUT_OK = "\x00d0ồng \x00fd";
        private const string BUT_RETRY = "Thử lại";
        private const string BUT_YES = "C\x00f3";
        private const long HCBT_ACTIVATE = 5L;
        private const int ID_BUT_ABORT = 3;
        private const int ID_BUT_CANCEL = 2;
        private const int ID_BUT_IGNORE = 5;
        private const int ID_BUT_NO = 7;
        private const int ID_BUT_OK = 1;
        private const int ID_BUT_RETRY = 4;
        private const int ID_BUT_YES = 6;
        private const int WH_CBT = 5;

        [DllImport("user32.dll")]
        private static extern IntPtr CallNextHookEx(IntPtr idHook, int nCode, IntPtr wParam, IntPtr lParam);
        [DllImport("gdi32.dll", EntryPoint="CreateFontA", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
        public static extern int CreateFont(int H, int W, int E, int O, int w, int I, int u, int S, int C, int OP, int CP, int Q, int PAF, string F);
        private IntPtr DialogHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if ((nCode >= 0) && (nCode == 5L))
            {
                SetDlgItemTextW(wParam, 1, Marshal.StringToHGlobalUni("\x00d0ồng \x00fd"));
                SetDlgItemTextW(wParam, 2, Marshal.StringToHGlobalUni("Th\x00f4i"));
                SetDlgItemTextW(wParam, 3, Marshal.StringToHGlobalUni("Huỷ"));
                SetDlgItemTextW(wParam, 4, Marshal.StringToHGlobalUni("Thử lại"));
                SetDlgItemTextW(wParam, 5, Marshal.StringToHGlobalUni("Bỏ qua"));
                SetDlgItemTextW(wParam, 6, Marshal.StringToHGlobalUni("C\x00f3"));
                SetDlgItemTextW(wParam, 7, Marshal.StringToHGlobalUni("Kh\x00f4ng"));
                UnhookWindowsHookEx(this._hook);
            }
            return CallNextHookEx(this._hook, nCode, wParam, lParam);
        }

        [DllImport("user32.dll", EntryPoint="FindWindowExA", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
        public static extern int FindWindowEx(IntPtr hWnd1, int hWnd2, string lpsz1, string lpsz2);
        public void HookDialog()
        {
            this.UnHookDialog();
            this._hook = SetWindowsHookEx(5, new HookProc(this.DialogHookProc), IntPtr.Zero, AppDomain.GetCurrentThreadId());
        }

        [DllImport("user32.dll", EntryPoint="SendMessageA", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        [DllImport("user32.dll")]
        private static extern bool SetDlgItemTextW(IntPtr hWnd, int nIDDlgItem, IntPtr lpString);
        [DllImport("user32.dll", CharSet=CharSet.Unicode)]
        private static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
        [DllImport("user32.dll", EntryPoint="SetWindowTextW", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
        public static extern int SetWindowText(IntPtr hwnd, IntPtr lpString);
        public void UnHookDialog()
        {
            if (!this._hook.Equals(IntPtr.Zero))
            {
                UnhookWindowsHookEx(this._hook);
            }
        }

        [DllImport("user32.dll")]
        private static extern int UnhookWindowsHookEx(IntPtr idHook);

        private delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
    }
}

